#pragma once


#include<iostream>

using namespace std;

namespace yyh
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{ }
	};

	template<class T>
	struct list_iterator
	{
		typedef list_node<T> Node;

		Node* _node;
		
		list_iterator(Node* node)
			:_node(node)
		{ }

		T& operator *()
		{
			return _node->_data;
		}

		list_iterator<T>& operator ++()
		{
			_node = _node->_next;
			return *this;
		}

		bool operator !=(const list_iterator<T>& it)
		{
			return _node != it._node;
		}

		bool operator ==(const list_iterator<T>& it)
		{
			return _node == it._node;
		}
	};

	template<class T>
	struct list_const_iterator
	{
		typedef list_node<T> Node;

		Node* _node;

		list_const_iterator(Node* node)
			:_node(node)
		{ }

		T& operator *()
		{
			return _node->_data;
		}

		list_const_iterator<T>& operator ++()
		{
			_node = _node->_next;
			return *this;
		}

		bool operator !=(const list_const_iterator<T>& it)
		{
			return _node != it._node;
		}

		bool operator ==(const list_const_iterator<T>& it)
		{
			return _node == it._node;
		}
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T> iterator;
		typedef list_const_iterator<T> const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin()
		{
			return const_iterator(_head->_next);
		}

		const_iterator end()
		{
			return const_iterator(_head);
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_back(const T& x)
		{
			Node* NewNode = new Node(x);
			Node* ptail = _head->_prev;
			ptail->_next = NewNode;
			NewNode->_prev = ptail;
			NewNode->_next = _head;
			_head->_prev = NewNode;
		}

	private:
		Node* _head;
	};
}